<?php
/**
 * CURL
 * @date 2018 07 13
 *
 */
namespace libraries;

class Curl{

	/**
	 * 通过URL获得端口
	 * @param $url
	 * @return int
	 */
	public static function getPort($url){
		preg_match('/:([\d]{4})\//i', $url, $matches);
		return isset($matches[1]) && !empty($matches[1]) ? $matches[1] : 80;//根据URL自动获取端口
	}

	public static function curl_get($url, $header = []){
		preg_match('/:([\d]{4})\//i', $url, $matches);
		$port = isset($matches[1]) && !empty($matches[1]) ? $matches[1] : 80;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_PORT, $port);
		if (is_array($header) && count($header) > 0) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

		$res = curl_exec($ch);
		$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		//echo $res;
		return $res;
	}

	public static function curl_get_request($url, $params = [], $header = []){

        $port = self::getPort($url);

		$uri = '';
		foreach ($params as $key => $value) {
			$uri .= '&' . $key . '=' . $value;
		}

		$uri = preg_replace('/^&/', '?', $uri);

		$url = $url . $uri;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_PORT, $port);
		if (is_array($header) && count($header) > 0) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

		$res = curl_exec($ch);
		$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		//echo $res;
		return $res;
	}

	/**
	 * FORMDATA
	 * @param $url
	 * @param $formdata
	 * @return mixed|string
	 */
	public static function post_formdata($url, $formdata){

		$port = self::getPort($url);

		$curl = curl_init();                                                // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $url);                                // 要访问的地址
		curl_setopt($curl, CURLOPT_PORT, $port);                            // 端口
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);                        // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);                        // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		// curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);                        // 使用自动跳转
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);                            // 自动设置Referer
		curl_setopt($curl, CURLOPT_POST, 1);                                // 发送一个常规的Post请求
		curl_setopt($curl, CURLOPT_POSTFIELDS, $formdata);                        // Post提交的数据包
		// curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']);    // 读取上面所储存的Cookie信息
		curl_setopt($curl, CURLOPT_TIMEOUT, 10);                            // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0);                                // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);                        // 获取的信息以文件流的形式返回

		$temp_data = curl_exec($curl);                                     // 执行操作
		if (curl_errno($curl)) {
			// echo 'Errno'.curl_error($curl);                                // 返回curl错误信息
			return 'error';
		}
		curl_close($curl);                                                    // 关键CURL会话
		return $temp_data;                                                    // 返回临时数据
	}

	/**
	 * RAW
	 * @param $url
	 * @param $raw
	 * @return mixed
	 */
	public static function raw($url, $raw){

		$port = self::getPort($url);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_PORT, $port);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		if (!empty($raw)) {
			$raw_array = [
				'X-AjaxPro-Method:ShowList',
				'Content-Type: application/json; charset=utf-8',
				'Content-Length: ' . strlen($raw)
			];
		}

		curl_setopt($ch, CURLOPT_HTTPHEADER, $raw_array);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

	/**
	 * HEADER RAW
	 * @param $url
	 * @param $header
	 * @param $raw
	 * @return mixed
	 */
	public static function  header_raw($url, $header, $raw_str){
		$port = self::getPort($url);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_PORT, $port);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		if (empty($header)) {
			$header = [];
		}

		if (!empty($raw_str)) {
			$raw_array = array_merge($header, [
				'X-AjaxPro-Method:ShowList',
				'Content-Type: application/json; charset=utf-8',
				'Content-Length: '.strlen($raw_str)
			]);
		}

		curl_setopt($ch, CURLOPT_HTTPHEADER, $raw_array);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $raw_str);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

}
?>